using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelVariants : MonoBehaviour
{
    public GameObject[] variants; // Массив уровней
    private int rand;
    private GameObject currentLevel;
    private PlayerController player;

    private GameObject Player;
    [SerializeField] private GameObject SecondPlayer;

    private void Start() {
        // Создание уровня из массива с помощью случайно сгенерированного индекса
        rand = Random.Range(0, variants.Length);
        Instantiate(currentLevel = variants[rand], transform.position, variants[rand].transform.rotation);
        player = FindObjectOfType<PlayerController>();

        if (PlayerPrefs.GetInt("playersCount") > 1) {
            Player = GameObject.FindGameObjectWithTag("Player");
            Instantiate(SecondPlayer, Player.transform.position, Quaternion.identity);
        }
    }

    // Обработка конца игры
    private void Update() {
        if (GameObject.FindGameObjectWithTag("door") == null) {
            PlayerPrefs.SetString("endGame", "win");
            PlayerPrefs.SetInt("result", player.score);
            SceneManager.LoadScene("DeathMenu");
        }
    }
}
